All files / remote remote_event.ts

100% Statements 50/50
100% Branches 7/7
100% Functions 17/17
100% Lines 45/45
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176                                2x   2x             2x             2x         730x       730x         730x     2x 24x     2x                       7x             2x               2x   2x   2x   2x                                                                           2x   2x 26x   2x 40x     2x 11x     2x 1x     2x 6x   2x   2x 1125x 1125x   2x 533x 548x 533x 533x     2x 616x 616x     2x 82x 82x     2x 28x           2x  
/**
 * Copyright 2017 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import { SnapshotVersion } from '../core/snapshot_version';
import { ProtoByteString, TargetId } from '../core/types';
import {
  documentKeySet,
  DocumentKeySet,
  MaybeDocumentMap
} from '../model/collections';
import { MaybeDocument } from '../model/document';
import { DocumentKey } from '../model/document_key';
import { emptyByteString } from '../platform/platform';
 
/**
 * An event from the RemoteStore. It is split into targetChanges (changes to the
 * state or the set of documents in our watched targets) and documentUpdates
 * (changes to the actual documents).
 */
export class RemoteEvent {
  constructor(
    /**
     * The snapshot version this event brings us up to, or MIN if not set.
     */
    readonly snapshotVersion: SnapshotVersion,
    /**
     * A map from target to changes to the target. See TargetChange.
     */
    readonly targetChanges: { [targetId: number]: TargetChange },
    /**
     * A set of which documents have changed or been deleted, along with the
     * doc's new values (if not deleted).
     */
    public documentUpdates: MaybeDocumentMap
  ) {}
 
  addDocumentUpdate(doc: MaybeDocument) {
    this.documentUpdates = this.documentUpdates.insert(doc.key, doc);
  }
 
  handleExistenceFilterMismatch(targetId: TargetId) {
    /*
     * An existence filter mismatch will reset the query and we need to reset
     * the mapping to contain no documents and an empty resume token.
     *
     * Note:
     *   * The reset mapping is empty, specifically forcing the consumer of the
     *     change to forget all keys for this targetID;
     *   * The resume snapshot for this target must be reset
     *   * The target must be unacked because unwatching and rewatching
     *     introduces a race for changes.
     */
    this.targetChanges[targetId] = {
      mapping: new ResetMapping(),
      snapshotVersion: SnapshotVersion.MIN,
      currentStatusUpdate: CurrentStatusUpdate.MarkNotCurrent,
      resumeToken: emptyByteString()
    };
  }
}
 
/**
 * Represents an update to the current status of a target, either explicitly
 * having no new state, or the new value to set. Note "current" has special
 * meaning for in the RPC protocol that implies that a target is both up-to-date
 * and consistent with the rest of the watch stream.
 */
export enum CurrentStatusUpdate {
  /** The current status is not affected and should not be modified. */
  None,
  /** The target must be marked as no longer "current". */
  MarkNotCurrent,
  /** The target must be marked as "current". */
  MarkCurrent
}
 
/**
 * A part of a RemoteEvent specifying set of changes to a specific target. These
 * changes track what documents are currently included in the target as well as
 * the current snapshot version and resume token but the actual changes *to*
 * documents are not part of the TargetChange since documents may be part of
 * multiple targets.
 */
export interface TargetChange {
  /**
   * The new "current" (synced) status of this target. Set to
   * CurrentStatusUpdateNone if the status should not be updated. Note "current"
   * has special meaning in the RPC protocol that implies that a target is
   * both up-to-date and consistent with the rest of the watch stream.
   */
  currentStatusUpdate: CurrentStatusUpdate;
 
  /**
   * A set of changes to documents in this target.
   */
  mapping: TargetMapping;
 
  /** The snapshot version that this target change brings us up to. */
  snapshotVersion: SnapshotVersion;
 
  /**
   * An opaque, server-assigned token that allows watching a query to be resumed
   * after disconnecting without retransmitting all the data that matches the
   * query. The resume token essentially identifies a point in time from which
   * the server should resume sending results.
   */
  resumeToken: ProtoByteString;
}
 
export type TargetMapping = ResetMapping | UpdateMapping;
 
const EMPTY_KEY_SET = documentKeySet();
 
export class ResetMapping {
  private docs: DocumentKeySet = EMPTY_KEY_SET;
 
  get documents(): DocumentKeySet {
    return this.docs;
  }
 
  add(key: DocumentKey) {
    this.docs = this.docs.add(key);
  }
 
  delete(key: DocumentKey) {
    this.docs = this.docs.delete(key);
  }
 
  isEqual(other: ResetMapping): boolean {
    return other !== null && this.docs.isEqual(other.docs);
  }
}
 
export class UpdateMapping {
  addedDocuments: DocumentKeySet = EMPTY_KEY_SET;
  removedDocuments: DocumentKeySet = EMPTY_KEY_SET;
 
  applyToKeySet(keys: DocumentKeySet): DocumentKeySet {
    let result = keys;
    this.addedDocuments.forEach(key => (result = result.add(key)));
    this.removedDocuments.forEach(key => (result = result.delete(key)));
    return result;
  }
 
  add(key: DocumentKey) {
    this.addedDocuments = this.addedDocuments.add(key);
    this.removedDocuments = this.removedDocuments.delete(key);
  }
 
  delete(key: DocumentKey) {
    this.addedDocuments = this.addedDocuments.delete(key);
    this.removedDocuments = this.removedDocuments.add(key);
  }
 
  isEqual(other: UpdateMapping): boolean {
    return (
      other !== null &&
      this.addedDocuments.isEqual(other.addedDocuments) &&
      this.removedDocuments.isEqual(other.removedDocuments)
    );
  }
}